package hashMap1;

import java.util.*;
import java.util.Map.Entry;

public class HashMapDemo {
	public static void main(String[] args) {
		Map countries = new HashMap();
		countries.put("CN", "中华人民共和国");
		countries.put("US", "美利坚合众国");
		countries.put("RU", "俄罗斯联邦");

		System.out.println("国家总数量：" + countries.size());
		System.out.println("CN对应的国家是：" + (String) countries.get("CN"));
		System.out.println("有没有CN的键？" + countries.containsKey("CN"));
		countries.remove("CN");
		System.out.println("有没有CN的键？" + countries.containsKey("CN"));
		System.out.println(countries.keySet()); // key的集合的返回值是Set
		System.out.println(countries.values()); // value的集合的返回值是Collection

		// 方法一:用keySet()方法
		Set keys = countries.keySet();
		// (1)：用iterator迭代器拿到所有的键
		Iterator iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			String value = (String) countries.get(key);
			System.out.println(key + "-" + value);
		}

		System.out.println();

		// (2)：用增强型for拿到所有的键
		for (Object obj : keys) {
			String key = (String) obj;
			String value = (String) countries.get(key);
			System.out.println(key + "-" + value);
		}
		
		System.out.println("***********************");
		
		// 方法二:用entrySet()方法
		Set set = countries.entrySet();
		// (1):用iterator迭代器拿到所有的键
		Iterator it = set.iterator();
		while(it.hasNext()){
			Map.Entry entry = (Map.Entry) it.next();
			String key = (String)entry.getKey();
			String value = (String)entry.getValue();
			System.out.println(key + "-" + value);
			//String value = (String)countries.get(key);
			//System.out.println(key + "-" + value);
		}
		
		System.out.println();
		
		// (2)：用增强型for拿到所有的键
		for(Object obj : set){
			String key = (String)((Map.Entry)obj).getKey();
			String value = (String)((Map.Entry)obj).getValue();
			System.out.println(key + "-" + value);
		}
	}

}
